//
//  MHNearByHomeTableViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/10/10.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHNearByHomeTableViewModel.h"
#import "MHHomeDiscoverItemViewModel.h"
#import "MHMomentDetailViewModel.h"
#import "MHCustomItemViewModel.h"
#import "MHNearByModel.h"
@interface MHNearByHomeTableViewModel()
//@property(nonatomic, strong) NSDateFormatter *formatter;
@property (nonatomic, readwrite, strong) MHDiscoverHomeModel *homeDiscover;
/// cellHeight
@property (nonatomic, readwrite, assign) CGSize cellSize;
@end

@implementation MHNearByHomeTableViewModel


- (void)initialize
{
    [super initialize];
    self.title =  LSSTRING("发现");
    
    self.rightImageStr = @"";
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    /// 没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = YES;
    
    @weakify(self);
    //圈子列表
    RAC(self, circles) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, circles) map:^(NSArray * circles) {
        @strongify(self)
        return [self dataSourceWithCircles:circles];
    }];
    
    
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
        @strongify(self);
        
        MHDiscoverHomeModel *circle = self.circles[indexPath.row];
        
        [AXAppDelegate sharedDelegate].currentDiscoverModel = circle;
        
        if(circle.vedioObj)
        {
            MHMoment *moment = [MHMoment new];
            moment.recentId = circle.recentId;
//            return  [[[self.services.client getRecentVideoPagedListByNearbyWithRecentId:circle.recentId locationLon:self.location.coordinate.longitude locationLat:self.location.coordinate.latitude pageNum:1 pageSize:3] mh_parsedResults] doNext:^(NSArray *modelArr) {
//                MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:modelArr,@"MHViewModelShowEnterBtn":@true, @"MHViewModelIsNearBy":@true,@"MHViewModelLocation":self.location,MHViewModelCommonModelUtilKey:moment}];
//                [self.services pushViewModel:viewModel animated:YES];
//            }];
            MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[],@"MHViewModelShowEnterBtn":@true, @"MHViewModelIsNearBy":@true,@"MHViewModelLocation":self.location,MHViewModelCommonModelUtilKey:moment,@"order":@(circle.n)}];
            [self.services pushViewModel:viewModel animated:YES];
            
        }else
        {
            
            MHMomentDetailViewModel *detail = [[MHMomentDetailViewModel alloc] initWithServices:self.services params:@{MHViewModelIDKey:@(circle.recentId),MHViewModelIsDetail:@(true)}];
            detail.rightTitle = circle.isInCircle ? @"进入圈子": @"加入圈子";
            [self.services pushViewModel:detail animated:YES];
            return [RACSignal empty];
        }
        return [RACSignal empty];
    }];
    
}


- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        self.showLoacation = [params[@"MHViewModelShowLocation"] boolValue];
        
    }
    return self;
}


- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    if(!self.location)
    {
        return [RACSignal empty];
    }
    
    NSArray * (^mapCircles)(NSArray *) = ^(NSArray *products) {
        if ((products == nil || [products isKindOfClass:[NSNull class]])) {
            products = @[];
        }
        if (page == 1) {
        } else {
            products = @[(self.circles ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
        }
        return products;
    };
    return [[[[MHHTTPService sharedInstance] getNearbyRecentPagedListWithPageNum:page  pageSize:self.perPage locationLat:self.location.coordinate.latitude locationLon:self.location.coordinate.longitude] mh_parsedResults] map:mapCircles];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithCircles:(NSArray *)circles {
    if (MHObjectIsNil(circles) || circles.count == 0) return @[];
    NSMutableArray *viewModels = [[circles.rac_sequence map:^(MHDiscoverHomeModel *discoverHome) {
        
        MHCustomItemViewModel *viewModel = [[MHCustomItemViewModel alloc] initWithModel:discoverHome];
        return viewModel;
    }].array mutableCopy];
    return viewModels ?: @[] ;
}
@end
